package com.hui.account.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hui.account.R;
import com.hui.account.entity.FileVo;

import java.util.List;

/**
 * 备份文件适配器
 */
public class FileAdapter extends BaseAdapter {

    Context context;

    List<FileVo> data;

    LayoutInflater inflater; // 获取视图

    public FileAdapter(Context context, List<FileVo> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int i, View view, ViewGroup parent) {
        // 1、获取视图
        view = inflater.inflate(R.layout.item_file, parent, false);
        ViewHolderUser entity = new ViewHolderUser(view);
        view.setTag(entity);

        // 2、填充数据
        FileVo file = data.get(i);
        entity.name.setText(file.getName());
        entity.size.setText(file.getSize());
        entity.time.setText(file.getTime());

        return view;
    }

    /**
     * 视图对应的实体类
     */
    static class ViewHolderUser {

        TextView name, size, time;

        public ViewHolderUser(View view) {
            name = view.findViewById(R.id.item_file_name);
            size = view.findViewById(R.id.item_file_size);
            time = view.findViewById(R.id.item_file_time);
        }
    }
}
